package com.foreknow.demo15;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 预处理PreparedStatement
 */
public class PreparedStatementDemo {
    public static void main(String[] args) {
        try {
            //1. 加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2. 获取连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic17?useUnicode=true&characterEncoding=utf8&useSSL=true","root","123456");
            //3. 获取PreparedStatement
//            PreparedStatement pstmt = conn.prepareStatement("select * from employee where username=? and password=?");
            PreparedStatement pstmt = conn.prepareStatement("insert into employee(username,password) values(?,?)");
            //   将?替换为具体的值
            pstmt.setString(1,"xxxxxx");
            pstmt.setString(2,"111111");
            //4. 执行查询
            int isRigth = pstmt.executeUpdate();
            System.out.println(isRigth);
//            List<Employee> list = new ArrayList<>();
//            while (rs.next()) {
//                Employee employee = new Employee();
//                employee.setId(rs.getInt("ID"));
//                employee.setUsername(rs.getString("USERNAME"));
//                employee.setPassword(rs.getString("PASSWORD"));
//                list.add(employee);
//            }
//            for (Employee e:list) {
//                System.out.println(e.getUsername());
//            }
            //5.关闭资源
//            rs.close();
            pstmt.close();
            conn.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
